<template>
	<view>
		<view class="nav-bar">
			<view :style="{ height: statusBarHeight }"></view>
			<view class="nav-bar-header">
				<view class="search" >
					<view class="city" v-if="location.city_name" 
						@click="go2page('/pages/store/selectorByCity')"
					>
						{{location.city_name}}
					</view>
					
					<image src="../../static/icon/search.png" mode=""></image>
					<view class="text" @click="searchClick">
						搜索产品
					</view>
				</view>
			</view>
		</view>


		<view class="nav">
			<scroll-view scroll-x scroll-with-animation :throttle="false" :scroll-left="scrollToLeft">
				<view class="nav-tabs">
					<view class="nat-item" v-for="(item,index) in categoryList" :key="index"
						:class="{'nav-active': tabsCurrent === index}" @click="tabClick(item,index)">
						<view class="nav-upper">
							<view class="nav-img">
								<image class="" :src="item.image" mode=""></image>
							</view>

							<!-- <view class="nav-text">{{item.name}}</view> -->
						</view>
						<view class="nav-text">
							{{item.name}}
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

		<view class="line"></view>

		<view class="content" :style="{'height': contentHeight + 'px'}">
			<view class="left">
				<scroll-view :scroll-y="true" :scroll-with-animation="true" :scroll-into-view="leftScrollViewId" :style="{'height': contentHeight + 'px'}">
					<view v-for="(item,index) in productList" :key="index"
						:class="{'left-item': index != categoryCurrent,'left-item-active': index === categoryCurrent }"
						@click="categoryClick(item,index)" :id="'left-current-' + index">
						<image :src="item.image" mode="" v-if="item.image"></image>
						<view class="text text-cut">
							{{ item.name }}
						</view>
						<view class="text-sub text-cut">
							{{ item.nickname }}
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="right">
				<scroll-view @scroll="rightScroll" :scroll-y="true" :scroll-with-animation="true"
					:scroll-into-view="scrollViewId" :style="{'height': contentHeight + 'px'}">
					<view class="right-item rightblock" v-for="(item,index) in productList" :key="item.id"
						:id="'current-' + index">
						<view class="title">
							<text>{{ item.name }} {{item.nickname}}</text>
						</view>
						<view class="right-li" v-for="(pitem,pindex) in item.product_list" :key="pindex"
							@click="jumpTo(pitem)">
							<image :src="pitem.cover_image"></image>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
	} from 'vuex';
	export default {
		components:{},
		data() {
			return {
				contentHeight: 750,
				tabsCurrent: 0,
				scrollToLeft: 0,
				categoryList: [],
				pid: 0,
				childrenCategoryList: [],
				category_id: 0,
				productList: [],
				scrollViewId: '',
				leftScrollViewId: '',
				categoryCurrent: 0,
				heightArr: [],
				statusBarHeight: uni.getWindowInfo().statusBarHeight + 'px'
			}
		},
		watch:{
			'cur_store':{
				handler:function(n,o){
					console.log("观察cur_store变化",n)
				},deep:true,immediate:true
			}
		},
		computed: {
			...mapGetters({
				location: 'user/location',
				cur_store: 'user/cur_store'
			}),
			navStyle() {
				// 获取设备系统信息，单位px
				const systemInfo = wx.getSystemInfoSync()
				// 状态栏高度
				const statusBarHeight = systemInfo.statusBarHeight
				// iOS真机 胶囊区高度 40
				let navigationHeight = 50
				// 胶囊区域高度，安卓48
				if (systemInfo.system.indexOf('Android') !== -1) {
					navigationHeight = 58
				}
				return {
					'margin-top': statusBarHeight + navigationHeight + 'rpx'
				}
			},
		},
		onLoad() {
			const systemInfo = wx.getSystemInfoSync()
			this.contentHeight = systemInfo.windowHeight - 200

			this.getParentCategory()
		},
		methods: {
			changeProduceList(storeId){
				let _this=this 
				_this.productList=[]
				_this.getProductList(storeId)
			},
			go2page(url){
				uni.navigateTo({
					url,
					success:(res)=>{
						let _event = res.eventChannel
						_event.on("changeStore",(store)=>{
							this.changeProduceList(store.id)
						})
					}
				})
			},
			changeStore(payload){
				
			},
			tabClick(item, index) {
				this.tabsCurrent = index
				this.pid = item.id
				this.categoryCurrent = 0
				this.leftScrollViewId = ''
				this.scrollViewId = ''
				this.getChildrenCategory()
			},
			categoryClick(item, index) {
				this.categoryCurrent = index
				this.scrollViewId = 'current-' + index
			},
			rightScroll(e) {
				let _this = this
				let st = e.detail.scrollTop
				let heightArr = _this.heightArr

				for (let i = 0; i < heightArr.length; i++) {
					if ((st > heightArr[i]) && (st < (heightArr[i + 1] - 5))) {
						_this.categoryCurrent = i
						_this.leftScrollViewId = 'left-current-' + i
					}
				}
			},
			jumpTo(item) {
				uni.navigateTo({
					url: '/pages/product/detail?id=' + item.id
				})
			},
			searchClick() {
				uni.navigateTo({
					url: '/pages/product/search'
				})
			},
			getParentCategory() {
				let _this = this
				_this.getCategoryList(0, (res) => {
					if (res.data.list.length > 0) {
						_this.categoryList = res.data.list
						_this.pid = _this.categoryList[0].id
						_this.getProductList()
						// _this.getChildrenCategory()
					}
				})
			},
			getChildrenCategory() {
				let _this = this
				_this.getCategoryList(_this.pid, (res) => {
					if (res.data.list.length > 0) {
						_this.childrenCategoryList = res.data.list
						_this.category_id = _this.childrenCategoryList[0].id
						_this.getProductList()
					} else {
						_this.childrenCategoryList = []
						_this.category_id = 0
						_this.productList = []
					}
				})
			},
			getCategoryList(pid = 0, callback) {
				let _this = this
				_this.$api.get('meet/product.category/index', {
					pid: pid
				}).then(res => {
					callback(res)
				}, fail => {})
			},
			getProductList(storeId) {
				let _this = this
				if(storeId==undefined)storeId = _this.cur_store.id
				_this.$api.get('meet/product.index/index', {
					category_id: _this.pid,
					storeId
				}).then(res => {
					_this.productList = res.data.list

					setTimeout(() => {
						let initArr = [0]
						let initNum = 0
						const query = uni.createSelectorQuery()
						query.selectAll('.rightblock').boundingClientRect()
						query.selectViewport().scrollOffset()
						query.exec(function(res) {
							res[0].map(val => {
								initNum += val.height //实现高度累加
								initArr.push(initNum) //将累加的高度存在数组里
							})

							_this.heightArr = initArr
						})
					}, 300)
				}, fail => {})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
		overflow-y: hidden;
	}

	.text-cut {
		overflow: hidden;
		-webkit-line-clamp: 1;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		text-overflow: ellipsis;
	}

	.nav-bar {

		.nav-bar-header {
			padding: 0 10px;
			height: 44px;
			width: 429rpx;
			display: flex;
			align-items: center;
			justify-content: flex-start;

			.search {
				background-color: #fff;
				width: 429rpx;
				// height: 44px;
				border-radius: 32rpx;
				border: 2rpx solid #DCDCDC;
				display: flex;
				align-items: center;
				justify-content: start;
				padding: 14rpx 0;
				padding-left: 32rpx;
				// margin-left: 18rpx;

				.city {
					font-weight: 800;
					font-size: 28rpx;
					color: #000000;
					border-right: 2px solid #BFBFBF;
					padding-right: 50rpx;
				}

				image {
					width: 32rpx;
					height: 32rpx;
					margin-left: 16rpx;
				}

				.text {
					margin-left: 8rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #8C8C8C;
				}
			}
		}
	}

	.nav {
		margin-top: 18px;
		width: 100%;
		overflow: auto;
		// padding-left: 36rpx;
		padding-bottom: 30rpx;
		// height: 40px;
		display: flex;
		align-items: center;

		.nav-tabs {
			display: flex;
			align-items: flex-start;
			overflow-x: auto;
			white-space: nowrap;
			padding-left: 36rpx;

			.nav-active {
				.nav-img {
					border: 2px solid #783a2f !important;
				}

				.nav-text {
					color: #783a2f !important
				}
			}

			.nat-item {
				margin-right: 50rpx;
				text-align: center;
				// min-width: 120rpx;
				// overflow: hidden;

				.nav-upper {
					display: flex;
					align-items: center;
					justify-content: center;

					.nav-img {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50rpx;
						overflow: hidden;
						border: 2px solid #fff;

						image {

							width: 100%;
							height: 100%;
						}
					}
				}

				.nav-text {
					font-weight: 800;
					font-size: 24rpx;
					color: #000000;
					margin-top: 10rpx;
					// margin-left: 2rpx;
				}

				.nav-lower {
					display: flex;
					align-items: center;
					justify-content: center;
					height: 20px;

					image {
						width: 50%;
					}
				}
			}
		}
	}

	.line {
		height: 10px;
		background-color: #F6F6F6;
	}

	.content {
		width: 100%;
		overflow: hidden;
		display: flex;
		justify-content: space-between;

		.left {
			width: 23%;
			background-color: #F0F0F0;
			overflow: scroll;

			.left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				padding: 26rpx 0 22rpx 0;
			}

			.left-item-active {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				padding: 26rpx 0 22rpx 0;
				background: #35D14A;
				border-radius: 0rpx 10rpx 10rpx 0rpx;

				.text {
					color: #fff;
				}

				.text-sub {
					color: #fff;
				}
			}

			image {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: 2rpx;
			}

			.text {
				width: 70%;
				text-align: center;
				font-weight: 400;
				font-size: 20rpx;
				color: #444444;
				margin-top: 5rpx;
			}

			.text-sub {
				width: 70%;
				text-align: center;
				font-weight: 400;
				font-size: 16rpx;
				color: #444444;
			}


		}

		.right {
			width: 77%;
			background-color: #FFFFFF;
			padding: 28rpx 24rpx;
			overflow: scroll;

			.title {
				font-weight: 800;
				font-size: 28rpx;
				color: #000000;
				letter-spacing: 1px;
				margin-bottom: 28rpx;
			}

			.right-li {
				width: 100%;
				border-radius: 16rpx;
				// border: 2rpx solid #707070;
				overflow: hidden;
				height: 202rpx;
				margin-bottom: 16rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.m-l-16 {
		margin-left: 16rpx;
	}
</style>